package com.dhwl.hsh.merchant.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.resp.UserAuthResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.merchant.HshApplication;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.model.impl.UserModel;
import com.dhwl.hsh.merchant.presenter.iview.IUserAuthView;
import com.dhwl.hsh.merchant.ui.activity.BaseActivity;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-13 12:55
 */
public class UserAuthPresenter extends BasePresenter<IUserAuthView> {

    private ReservoirUtils reservoirUtils;

    public UserAuthPresenter() {
        this.reservoirUtils = new ReservoirUtils();
    }


    public void getUserAuth() {
        Subscriber<HttpResponse<UserAuthResp>> subscriber = new Subscriber<HttpResponse<UserAuthResp>>() {
            @Override
            public void onCompleted() {
                if (UserAuthPresenter.this.mCompositeSubscription != null)
                    UserAuthPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("getMerchant", e.getMessage());
                ((BaseActivity) UserAuthPresenter.this.getMvpView()).hideWaitingDialog();
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
                HshException exception = new HshException(e.hashCode(), e.getMessage());
                UserAuthPresenter.this.getMvpView().onUserAuthFailure(exception);
            }

            @Override
            public void onNext(HttpResponse<UserAuthResp> response) {
                if (response.getStatus() != 0) {
                    ToastUtils.show((Context) getMvpView(), response.getDescribe(), Toast.LENGTH_LONG);
                    HshException exception = new HshException(response.getStatus(), response.getDescribe());
                    UserAuthPresenter.this.getMvpView().onUserAuthFailure(exception);
                } else {
                    UserAuthResp userAuthResp = response.getData();
                    reservoirUtils.refresh("merchant_auth", userAuthResp);
                    UserAuthPresenter.this.getMvpView().onUserAuthSuccess(userAuthResp);

                }
            }
        };
        this.mCompositeSubscription.add(UserModel.getInstance().getUserAuth().subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) UserAuthPresenter.this.getMvpView()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) UserAuthPresenter.this.getMvpView()).hideWaitingDialog();
            }
        }).subscribe(subscriber));
    }
}
